package fr.nts.framework.front.interceptor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.nts.framework.database.jpa.model.common.SimpleMenu;
import fr.nts.framework.front.service.MenuManager;

/**
 * Intercepteur spécifique au menu
 * 
 * @author sco
 * @version 1.0.0
 */
public class MenuInterceptor extends AbstractInterceptor{

	private static final String PARENT_MENU_LIST_MODEL = "parent_menu_list";
	private static final String CURRENT_MENU_MODEL = "current_menu";
	
	private static final String RELOAD_PARAM = "rm";
	private static final String CURRENT_MENU_PARAM = "cm";
	
	private static final String DEFAULT_CURRENT_MENU_COOKIE = "nts_current_menu";
	
	/**
	 * Le cookie pour gérer le menu
	 */
	private String ntsMenuCookie = DEFAULT_CURRENT_MENU_COOKIE;
	
	private final static Logger log = LoggerFactory.getLogger(MenuInterceptor.class);
	
	@Resource
	private MenuManager menuManager;
	
	
	/*
	 * (non-Javadoc)
	 * @see fr.nts.framework.front.interceptor.AbstractInterceptor#intercept(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	void intercept(HttpServletRequest request,
			       HttpServletResponse response){
		
		String selectedMenu = request.getParameter(CURRENT_MENU_PARAM);
		
		// Gére le changement de menu
		if(!StringUtils.isEmpty(selectedMenu)){
			log.info("MENU INTERCEPTOR - On a séléctionné le menu: {}", selectedMenu);
			Cookie cookie = new Cookie(selectedMenu, 
					                   selectedMenu);
			cookie.setMaxAge(-120);
			response.addCookie(cookie);
			
		// Reprise du derniere menu
		}else{
			Cookie cookie = getNTSCurrentMenuCookie(request);
			if(cookie != null){
				selectedMenu = cookie.getValue();
			}
		}
		
		// Mise en request du menu séléctionné
		SimpleMenu menu = menuManager.findMenuById(selectedMenu);
		
		if(menu != null){
			log.debug("Le menu actuellement selectionné est: {}.", menu);
			
			request.setAttribute(CURRENT_MENU_MODEL, 
					             menu);
		}else{
			log.debug("Il n'y aucun menu de selectionné.");
		}
	}

	
	/*
	 * (non-Javadoc)
	 * @see fr.nts.framework.front.interceptor.AbstractInterceptor#finish(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	void finish(HttpServletRequest request){
		if(Boolean.TRUE.toString().equals(request.getParameter(RELOAD_PARAM))){
			log.info("MENU INTERCEPTOR - On recharge les menus.");
			menuManager.reloadMenus();
		}
		
		List<SimpleMenu> menus = menuManager.getParentMenus();
		
		log.debug("MENU INTERCEPTOR - Liste des menus récupérés: {}", menus);
		
		request.setAttribute(PARENT_MENU_LIST_MODEL, 
				             menus);
	}
	
	
	/**
	 * Recupère le cookie du menu
	 */
	private Cookie getNTSCurrentMenuCookie(HttpServletRequest request){
		for(Cookie cookie: request.getCookies()){
			if(cookie.getName().equals(ntsMenuCookie)){
				return cookie;
			}
		}
		
		return null;
	}
	
	
	// GETTER / SETTER
	public String getNtsMenuCookie(){
		return ntsMenuCookie;
	}

	public void setNtsMenuCookie(String ntsMenuCookie){
		this.ntsMenuCookie = ntsMenuCookie;
	}
}